﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Conditional Color Replacement
    /// Use this
    /// </summary>
    public class ConditionalColorReplacement : BaseImageFilter
    {
        protected byte m_nMinR = 0;
        protected byte m_nMinG = 0;
        protected byte m_nMinB = 0;
        protected byte m_nMaxR = 0;
        protected byte m_nMaxG = 0;
        protected byte m_nMaxB = 0;

        /// <summary>
        /// Default constructor
        /// </summary>
        public ConditionalColorReplacement()
            : this(200, 200, 200, 219, 219, 219)
        {
        }

        /// <summary>
        /// primary constructor
        /// </summary>
        /// <param name="_colorMin">The _color min.</param>
        /// <param name="_colorMax">The _color max.</param>
        public ConditionalColorReplacement(Color _colorMin, Color _colorMax)
            : this(_colorMin.R, _colorMin.G, _colorMin.B, _colorMax.R, _colorMax.G, _colorMax.B)
        {
        }

        /// <summary>
        /// Conditional color replacement
        /// </summary>
        /// <param name="_nMinR"></param>
        /// <param name="_nMinG"></param>
        /// <param name="_nMinB"></param>
        /// <param name="_nMaxR"></param>
        /// <param name="_nMaxG"></param>
        /// <param name="_nMaxB"></param>
        public ConditionalColorReplacement(byte _nMinR, byte _nMinG, byte _nMinB, byte _nMaxR, byte _nMaxG, byte _nMaxB)
        {
            m_nMinR = _nMinR;
            m_nMinG = _nMinG;
            m_nMinB = _nMinB;

            m_nMaxR = _nMaxR;
            m_nMaxG = _nMaxG;
            m_nMaxB = _nMaxR;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_conditionalColorReplacement">The _conditional color replacement.</param>
        internal ConditionalColorReplacement(ConditionalColorReplacement _conditionalColorReplacement)
        {
            this.m_nMinR = _conditionalColorReplacement.m_nMinR;
            this.m_nMinG = _conditionalColorReplacement.m_nMinG;
            this.m_nMinB = _conditionalColorReplacement.m_nMinB;

            this.m_nMaxR = _conditionalColorReplacement.m_nMaxR;
            this.m_nMaxG = _conditionalColorReplacement.m_nMaxG;
            this.m_nMaxB = _conditionalColorReplacement.m_nMaxB;
        }

        /// <summary>
        /// Gets or sets MinR
        /// </summary>
        public byte MinR
        {
            get { return m_nMinR; }
            set { m_nMinR = value; }
        }

        /// <summary>
        /// Gets or sets MinG
        /// </summary>
        public byte MinG
        {
            get { return m_nMinG; }
            set { m_nMinG = value; }
        }

        /// <summary>
        /// Gets or MinB
        /// </summary>
        public byte MinB
        {
            get { return m_nMinB; }
            set { m_nMinB = value; }
        }

        /// <summary>
        /// Gets or sets MaxR
        /// </summary>
        public byte MaxR
        {
            get { return m_nMaxR; }
            set { m_nMaxR = value; }
        }

        /// <summary>
        /// Gets or sets MaxG
        /// </summary>
        public byte MaxG
        {
            get { return m_nMaxG; }
            set { m_nMaxG = value; }
        }

        /// <summary>
        /// Gets or sets MaxB
        /// </summary>
        public byte MaxB
        {
            get { return m_nMaxB; }
            set { m_nMaxB = value; }
        }

        /// <summary>
        /// Executes the filter
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            Color color = Color.Transparent;
            int r = 0;
            int g = 0;
            int b = 0;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);

                    r = color.R;
                    g = color.G;
                    b = color.B;

                    if (r > m_nMaxR)
                    {
                        bitmap.SetPixel(x, y, ColorFunctions.MaxRed(color));
                    }
                    else if (r < m_nMinR)
                    {
                        bitmap.SetPixel(x, y, ColorFunctions.ZeroRed(color));
                    }

                    if (g > m_nMaxG)
                    {
                        bitmap.SetPixel(x, y, ColorFunctions.MaxGreen(color));
                    }
                    else if (g < m_nMinG)
                    {
                        bitmap.SetPixel(x, y, ColorFunctions.MaxGreen(color));
                    }

                    if (b > m_nMaxB)
                    {
                        bitmap.SetPixel(x, y, ColorFunctions.MaxBlue(color));
                    }
                    else if (b < m_nMinB)
                    {
                        bitmap.SetPixel(x, y, ColorFunctions.ZeroBlue(color));
                    }
                }
            }
            return bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ConditionalColorReplacement(this);
        }
    }
}
